//
//  YHEmotionCollectionView.swift
//  XLWB
//
//  Created by key on 16/7/4.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

private let YHEmotionCollectionViewID = "YHEmotionCollectionViewCell"

class YHEmotionCollectionView: UICollectionView {

    //MARK: - 入口
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        backgroundColor = UIColor.whiteColor()
        
        //布局UI
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 布局UI
    func setupUI() {
        
        //设置代理
        dataSource = self
        
        //注册cell
        registerClass(YHEmotionCollectionViewCell.self, forCellWithReuseIdentifier: YHEmotionCollectionViewID)
        
        //设置flowLayout
        let flowLayout = collectionViewLayout as? UICollectionViewFlowLayout
        
        flowLayout?.itemSize = CGSize(width: screenW, height: 216 - 37)
            
        flowLayout?.minimumInteritemSpacing = 0
        
        flowLayout?.minimumLineSpacing = 0
        
        flowLayout?.scrollDirection = .Horizontal
        
    }
    
}


//MARK: - 数据源
extension YHEmotionCollectionView: UICollectionViewDataSource {
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        
        return YHEmotionData.shareEmotion.emotionAllArray.count
    }
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return YHEmotionData.shareEmotion.emotionAllArray[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        //获取cell
        let cell = dequeueReusableCellWithReuseIdentifier(YHEmotionCollectionViewID, forIndexPath: indexPath) as! YHEmotionCollectionViewCell
        
        //数据传输
        cell.emotions = YHEmotionData.shareEmotion.emotionAllArray[indexPath.section][indexPath.item]
        
        cell.backgroundColor = self.backgroundColor
        
        //返回
        return cell
        
        
    }
    
}

















